package com.codes.core.common.thread.pool;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.concurrent.Future;

/**
 * @Description 
 * @Date 2020/8/26 10:23
 * @Created  peanut
 */
@Slf4j
//@Service
@Component
public class TestThread {

    @Async("taskExecutor")
    public void printWord(){

        log.info(Thread.currentThread().getName());
        log.info(Thread.currentThread().getId()+"");
        System.out.println("线程" + Thread.currentThread().getName() + "正在执行任务" );
    }
    @Async("taskExecutor")
    public void doTaskTwo() throws Exception {
        log.info("开始做任务二");
        long start = System.currentTimeMillis();
   //     log.info(stringRedisTemplate.randomKey());
        Thread.sleep(50);
        long end = System.currentTimeMillis();
        log.info( Thread.currentThread().getName() + "完成任务二，耗时：" + (end - start) + "毫秒");
    }


    // 有返回值
    @Async
    public Future<Long> asyncTest() {
        long startTime = new Date().getTime();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long endTime = new Date().getTime();
        long time = endTime - startTime;
        return new AsyncResult(time);
    }



}